const app = getApp(); //引入导航

var util = require('../../utils/common.js'); //引入公共JS


import { JqSearch } from 'jqSearch.model.js';
var jqSearch = new JqSearch();
Page({
  data: {
    showIcon: true,
    //显示返回键以及首页
    cityData: "全国",
    cityFlag: true,
    //城市
    SearchClose: true,
    SearchTo: false
  },
  onLoad: function (options) {
    var inputdata = options.inputdata;
    var source = options.source;

    if (source) {
      tt.setStorageSync('source', source);
    }

    this.setData({
      inputdata: inputdata
    });

    this._onLoad(inputdata);
  },
  //关闭页面时隐藏城市弹框
  onHide: function () {
    this.setData({
      cityFlag: true,
      //关闭城市
      searchFlag: true,
      //关闭搜索列表
      inputdata: "",
      //清空输入框的值
      SearchClose: true,
      SearchTo: false
    });
  },
  _onLoad: function (inputdata) {
    this.data.inputdata = inputdata;
    this.data.datarow = inputdata;

    if (inputdata == '') {
      this.setData({
        searchFlag: true
      });
    } else {
      //搜索页面
      jqSearch.getSearchFor(this.data.datarow, res => {
        //console.log(res)
        this.setData({
          'SearchFor': res,
          searchFlag: false
        });
      });
    }
  },
  //打开搜索
  searchValue: function (e) {
    var _data = e.detail.value;
    var patt3 = /^[\u4e00-\u9fa5]{2,20}$/; //正则验证汉字不能少两个，不能多余20个

    this.setData({
      //保存输入框的值
      inputdata: e.detail.value
    });
    this.data.datarow = _data;

    if (_data == '') {
      this.setData({
        SearchClose: true,
        SearchTo: false,
        inputdata: ""
      });
    } else if (patt3.test(_data)) {
      //搜索页面
      jqSearch.getSearchFor(this.data.datarow, res => {
        console.log(res);
        this.setData({
          'SearchFor': res,
          SearchClose: false,
          SearchTo: true
        });
      });
    }
  },
  //点击关闭按钮关闭搜索框
  toClose: function () {
    this.setData({
      SearchClose: true,
      SearchTo: false,
      inputdata: "" //清空输入框的值

    });
  },
  // 拨打电话
  calling: function () {
    util.calling();
  },
  //分享
  onShareAppMessage: function () {
    util.sharePage();
  },
  //回到顶部
  goTop: function () {
    // 一键回到顶部
    util.goTop();
  }
});